package com.davidhigenbottam.HeatMap.client;

import com.davidhigenbottam.HeatMap.shared.Query;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UI {
	
	public static String PRIVATE_INSTITUTE_ICON="http://maps.google.com/mapfiles/kml/paddle/blu-circle.png";
	public static String PUBLIC_INSTITUTE_ICON="http://maps.google.com/mapfiles/kml/paddle/red-circle.png";
	private static UI instance= null;
	
	
	//private DockPanel mainDockPanel = new DockPanel();
	private FlowPanel mainPanel = new FlowPanel();
	private AbsolutePanel absolutePanel = new AbsolutePanel();
	
	public VerticalPanel customLogArea= new VerticalPanel();
	
	private FlexTable queryFlexTable= new FlexTable();
	 
	private DecoratorPanel dataSourceDecoratorPanel = new DecoratorPanel();
	private DecoratorPanel queryDecoratorPanel = new DecoratorPanel();
	private DecoratorPanel overlaysDecoratorPanel = new DecoratorPanel();
	private DecoratorPanel kmlDecoratorPanel = new DecoratorPanel();
	private DecoratorPanel mapDecoratorPanel = new DecoratorPanel();
	private DecoratorPanel tableDecoratorPanel = new DecoratorPanel();
	
	private Disclosures disclosures= new Disclosures();
	
	private HorizontalPanel tableHPanel= new HorizontalPanel();
	
	
	private VerticalPanel overlaysPanel= new VerticalPanel();
	private FlowPanel kmlFlowPanel= new FlowPanel();
	private HorizontalPanel hPanel = new HorizontalPanel();
	
	private DialogBox waitingDialogBox = new DialogBox(false);
	
	public MapPanel map;
	
	
	// query ui elements
	
	private HTML mapTitle= new HTML();
	Label yearLabel = new Label("Year:");
	private ListBox year = new ListBox();
	
	Label levelOfEducationLabel =  new Label("Level of Education:", true);
	private ListBox levelOfEducation = new ListBox();
	Label instTypeLabel = new Label("Type of Institute:", true);
	private ListBox instituteType = new ListBox();
	
	Label instOwnershipLabel = new Label("Ownership of Institute:", true);
	private ListBox instituteOwnership = new ListBox();
	
	//Label typeOfOverlayLabel = new Label("Method of data visualisation:", true);
	//private ListBox typeOfOverlay = new ListBox();
	
	
	private Button createVisualisationButton = new Button("Generate Data Visualisation");	
	
	private ListBox overLayListBox= new ListBox();
//	public LatLng mapDefaultCenter=null;
	
	
	//InstitutionsCheckBox showInstitutions= new InstitutionsCheckBox();
	HTML institutionKeyHtml= new HTML("Private:<img src=\""+PRIVATE_INSTITUTE_ICON+"\" width=\"16\" height=\"16\"/>"
			+"Public:<img src=\""+PUBLIC_INSTITUTE_ICON+"\"  width=\"16\" height=\"16\"/>");
	
	private UI(){
		
		//dataSourceDecoratorPanel.set;
		//queryDecoratorPanel.setSize("100px","50px");
		//overlaysDecoratorPanel.setSize("100px","50px");
		
		waitingDialogBox.setText("Loading...");
		waitingDialogBox.setGlassEnabled(true);
		waitingDialogBox.center();
		waitingDialogBox.hide();
		
		mainPanel.add(absolutePanel);
		
		//add these now so they are on top
		mainPanel.add(mapTitle);
		mainPanel.add(institutionKeyHtml);
		mainPanel.add(tableDecoratorPanel);
		
		hideInstitutionKey();
		
		hPanel.setSpacing(3);
		
		absolutePanel.setSize(HeatMap.UI_WIDTH, HeatMap.UI_HEIGHT);
		absolutePanel.ensureDebugId("hmMainPanel");
		
		disclosures.setContent(Disclosures.DATA_SOURCE,DataSources.getInstance().getPanel());
		dataSourceDecoratorPanel.add(Disclosures.DATA_SOURCE);
		
		hPanel.add(dataSourceDecoratorPanel);
		
		yearLabel.setStyleName("align-right");
		
		//goButton.setVisible(false);
		queryFlexTable.setWidget(1, 0, yearLabel);
		queryFlexTable.setWidget(1, 1, year);
		queryFlexTable.setWidget(2, 0, levelOfEducationLabel);
		queryFlexTable.setWidget(2, 1, levelOfEducation);
		queryFlexTable.setWidget(3, 0, instTypeLabel);
		queryFlexTable.setWidget(3, 1, instituteType);
		queryFlexTable.setWidget(4, 0, instOwnershipLabel);
		queryFlexTable.setWidget(4, 1, instituteOwnership);
		
		//queryFlexTable.setWidget(5, 0, typeOfOverlayLabel);
		//queryFlexTable.setWidget(5, 1, typeOfOverlay);
		

		queryFlexTable.setWidget(5,0,createVisualisationButton);
		queryFlexTable.getFlexCellFormatter().setColSpan(5, 0, 2);
		
		disclosures.setContent(Disclosures.QUERY,queryFlexTable);
		queryDecoratorPanel.add(Disclosures.QUERY);
		hPanel.add(queryDecoratorPanel);
		
		
		//Overlays
		
		//showInstitutions.setWidth("150px");
		//overlaysPanel.add(showInstitutions);
		overlaysPanel.add(overLayListBox);

		disclosures.setContent(Disclosures.OVERLAYS,overlaysPanel);
		overlaysDecoratorPanel.add(Disclosures.OVERLAYS);
		hPanel.add(overlaysDecoratorPanel);
		
		
		disclosures.setContent(Disclosures.KML,kmlFlowPanel);
		kmlDecoratorPanel.add(Disclosures.KML);
		hPanel.add(kmlDecoratorPanel);
		
		
		tableDecoratorPanel.add(tableHPanel);
		//mainDockPanel.add(tableDecoratorPanel, DockPanel.EAST);
		
		

		mapDecoratorPanel.addStyleName("map");
		tableDecoratorPanel.addStyleDependentName("table");
		tableDecoratorPanel.setVisible(false);
		
	
	}
	
	public static UI getInstance(){
		if(instance==null)
			instance= new UI();
		
		return instance;	
	}	
	
	public void addUrls(String kmlUrl, String excelUrl, String name){
		kmlFlowPanel.add(new HTML("<span style=\"font-size:10px;\"> "+name+"</span> <a style=\"font-size:10px;\" target=\"blank\" href=\""+kmlUrl+"\">kml</a>"
				+" <a style=\"font-size:10px;\" target=\"blank\" href=\""+excelUrl+"\">excel</a>"));
	}
	public void addKmlUrl(String kmlUrl, String name){
		kmlFlowPanel.add(new HTML("<a style=\"font-size:10px;\" target=\"blank\" href=\""+kmlUrl+"\">"+name+"</a>"));
	}	
	

	public void showOverlays(){
		overlaysDecoratorPanel.setVisible(true);
	}


	public void showQueryControls() {
		queryDecoratorPanel.setVisible(true);
		disclosures.open(Disclosures.QUERY);
		//queryFlexTable.setVisible(true);
		
		//goButton.setVisible(true);
		//overlays.getListBox().setVisible(true);
		
	}
	public void hideQueryControls() {
		queryDecoratorPanel.setVisible(false);
		//queryFlexTable.setVisible(false);
		//goButton.setVisible(false);
		
	}
	


	public Widget getParentPanel() {
		return mainPanel;
	}

	public void showWaitingDialog() {
		waitingDialogBox.show();
	}
	public void hideWaitingDialog() {
		waitingDialogBox.show();
	}

	
	public void addProvinceTable(ProvinceTable pt){
		tableHPanel.add(pt.getCellTable());
		tableDecoratorPanel.setVisible(true);
	}
	public void addInstitutionTable(InstitutionTable it){
		tableHPanel.add(it.getCellTable());
		tableDecoratorPanel.setVisible(true);
	}	

	public MapPanel getMap() {
		return map;
	}

	public void setMap(MapPanel map) {
		this.map = map;
	}



	public ListBox getOverLayListBox() {
		return overLayListBox;
	}

	public void setOverLayListBox(ListBox overLayListBox) {
		this.overLayListBox = overLayListBox;
	}

	public Button getCreateHeatMapButton() {
		return createVisualisationButton;
	}

	public void setCreateHeatMapButton(Button goButton) {
		this.createVisualisationButton = goButton;
	}
	
	
	
	public void addLevelOfEducation(String item, String value){
		levelOfEducation.addItem(item, value);
	}
	public void clearLevelOfEducation(){
		levelOfEducation.clear();
	}
	public void showLevelOfEducation(){
		levelOfEducation.setVisible(true);
		levelOfEducationLabel.setVisible(true);
	}	
	public void hideLevelOfEducation(){
		levelOfEducation.setVisible(false);
		levelOfEducationLabel.setVisible(false);
	}

	public void showYear() {
		year.setVisible(true);
		yearLabel.setVisible(true);
	}
	public void hideYear() {
		year.setVisible(false);
		yearLabel.setVisible(false);
	}
	
	public void hideInstituteType() {
		instituteType.setVisible(false);
		instTypeLabel.setVisible(false);
	}
	public void showInstituteType() {
		instituteType.setVisible(true);
		instTypeLabel.setVisible(true);
	}	
	public void clearInstituteType() {
		instituteType.clear();
	}
	public void addInstituteType(String item, String value){
		instituteType.addItem(item, value);
	}
	
	
	public void hideInstituteOwnership() {
		instituteOwnership.setVisible(false);
		instOwnershipLabel.setVisible(false);
	}
	public void showInstituteOwnership() {
		instituteOwnership.setVisible(true);
		instOwnershipLabel.setVisible(true);
	}
	
//	public void hideTypeOfOverlay() {
//		typeOfOverlay.setVisible(false);
//		typeOfOverlayLabel.setVisible(false);
//	}
//	public void showTypeOfOverlay() {
//		typeOfOverlay.setVisible(true);
//		typeOfOverlayLabel.setVisible(true);
//	}	
	
	
	public void clearInstituteOwnership() {
		instituteOwnership.clear();
	}
	public void addInstituteOwnership(String item, String value){
		instituteOwnership.addItem(item, value);
	}
	
//	public void clearTypeOfOverlay() {
//		typeOfOverlay.clear();
//	}
//	public void addTypeOfOverlay(String item, String value){
//		typeOfOverlay.addItem(item, value);
//	}
	
	public void hideWaiting(){
		waitingDialogBox.hide();
	}
	
	public void stopWaiting(){
		getCreateHeatMapButton().setEnabled(true);
		hideWaiting();
		disclosures.close(Disclosures.QUERY);
	}
	
	public void showWaiting() {
		getCreateHeatMapButton().setEnabled(false);
		showWaitingDialog();
	}

	public ListBox getYear() {
		return year;
	}

	public void setYear(ListBox year) {
		this.year = year;
	}

	public ListBox getLevelOfEducation() {
		return levelOfEducation;
	}

	public void setLevelOfEducation(ListBox levelOfEducation) {
		this.levelOfEducation = levelOfEducation;
	}	
	

	public void addYears() {
		for(String y: Query.years)
			year.addItem(y);
		
	}
	
//	public void createMap() {
//
//		mapDefaultCenter = LatLng.newInstance(HeatMap.DEFAULT_LAT, HeatMap.DEFAULT_LNG);
//		map= new MapWidget(mapDefaultCenter, HeatMap.MAP_DEFAULT_ZOOM);
//		map.setSize(HeatMap.MAP_WIDTH, HeatMap.MAP_HEIGHT);
//		map.setUIToDefault();
//		mapDecoratorPanel.add(map);
//		absolutePanel.add(mapDecoratorPanel,0, HeatMap.UI_TOP_SPACE_HEIGHT);
//		absolutePanel.add(hPanel, 0, 0);
//
//	}

	
	public void createMap(){
		
		map= new MapPanel(); 
		
		mapDecoratorPanel.add(map);
		absolutePanel.add(mapDecoratorPanel,0, HeatMap.UI_TOP_SPACE_HEIGHT);
		absolutePanel.add(hPanel, 0, 0);
		
		map.createMap(); 

	}
	
//native void createJSMap() /*-{
//    var latlng = new $wnd.google.maps.LatLng(-34.397, 150.644);
//    var myOptions = {
//      zoom: 8,
//      center: latlng,
//      mapTypeId: $wnd.google.maps.MapTypeId.ROADMAP
//    };
//    var map = new $wnd.google.maps.Map($doc.getElementById("map_canvas"), myOptions);
//}-*/;	
//	
	
	
//	public void createMap(){
//		final MapOptions options = new MapOptions();
//	    // Zoom level. Required
//	    options.setZoom(8);
//	    // Open a map centered on Cawker City, KS USA. Required
//	    options.setCenter(new LatLng(39.509, -98.434));
//	    // Map type. Required.
//	    options.setMapTypeId(new MapTypeId().getRoadmap());
//	    
//	    // Enable maps drag feature. Disabled by default.
//	    options.setDraggable(true);
//	    // Enable and add default navigation control. Disabled by default.
//	    options.setNavigationControl(true);
//	    // Enable and add map type control. Disabled by default.
//	    options.setMapTypeControl(true);
//	    mapWidget = new MapWidget(options);
//	    mapWidget.setSize("800px", "600px");
//	    
//	    
//	    mapDecoratorPanel.add(mapWidget);
//		absolutePanel.add(mapDecoratorPanel,0, HeatMap.UI_TOP_SPACE_HEIGHT);
//		absolutePanel.add(hPanel, 0, 0);
//	}	
//	

	public ListBox getInstitutionType() {
		return this.instituteType;
	}
	
	public ListBox getInstitutionOwnership() {
		return this.instituteOwnership;
	}
//	public ListBox getTypeOfOverlay() {
//		return this.typeOfOverlay;
//	}
	
	public void setMapTitle(String title){
		this.mapTitle.setHTML(title);
	}

	public void updateMapTitle(Query q) {
		setMapTitle("<p>"+q.getLongDescription()+"</p>");
		
	}

	public void showInstitutionKey() {
		institutionKeyHtml.setVisible(true);
	}
	public void hideInstitutionKey() {
		institutionKeyHtml.setVisible(false);
	}


	
	


	

}
